package odgs.manager.filedb;

import java.io.Serializable;

import odgs.GridNode;
import odgs.LoginedGridNode;

public class FileDBGridNode implements Serializable
{
	private String id;
	private long allowedSize;
	private long usedSize;
	private long reservedSize;
	
	public FileDBGridNode(String id, long allowedSize, long usedSize)
	{
		this.id = id;
		this.allowedSize = allowedSize;
		this.reservedSize = 0; 
		this.usedSize = usedSize;
	}

	@Override
	public boolean equals(Object arg0)
	{
		FileDBGridNode node = (FileDBGridNode) arg0;
		if(node.getId().equals(id))
			return true;
		else
			return false;
	}

	public void freeReservedSize(long size)
	{
		if(reservedSize - size >= 0)
			reservedSize -= size;
		else
			reservedSize = 0;
	}

	public void freeStorage(long size)
	{
		if(usedSize - size >= 0)
			usedSize -= size;
		else
			usedSize = 0;
	}

	public long getAllowedSize()
	{
		return allowedSize;
	}

	public String getId()
	{
		return id;
	}
	
	public long getReservedSize() 
	{
		return reservedSize;
	}
	
	public long getUsedSize()
	{
		return usedSize;
	}

	public void reserveStorage(long size)
	{
		reservedSize += size;
	}

	public void useStorage(long size)
	{
		usedSize += size;
	}

	public GridNode getGridNode()
	{
		return new GridNode(id,allowedSize,getUsedSize(),getReservedSize());
	}

	public long getFreeSize()
	{
		return getAllowedSize() - (getUsedSize() + getReservedSize());
	}
}
